package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询，使用PageHandler，将持久层返回的多条student对象装到Page集合内
     * 这个集合对象有total属性记录了装了多少条数据
     * p本身是对象数据，p和p的total属性来对PageBean的两个属性进行赋值
     * @param sqp
     * @return
     */
    @Override
    public PageBean getAllStudent(StudentQueryParam sqp) {
        PageBean pageBean = new PageBean();

        PageHelper.startPage(sqp.getPage(),sqp.getPageSize());
        Page<Student> p =  studentMapper.selectStudents(sqp);
        pageBean.setTotal(p.getTotal());//p的一个属性Total记录有多少条。
        pageBean.setRows(p);
        return (pageBean);
    }

    /**
     * 添加学生
     * @param student
     */
    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insertStudent(student);
    }

    /**
     * 根据id查询学生信息
     * @param id
     * @return
     */
    @Override
    public Student getStudentById(Integer id) {
        return studentMapper.selectStudentById(id);
    }

    /**
     * 修改学生信息
     * @param student
     */
    @Override
    public void modifyStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    @Override
    public void deleteStudents(List<Integer> ids) {
        studentMapper.deleteStudentsByIds(ids);
    }

    /**
     * 违纪处理
     * @param id
     * @param score
     */
    @Override
    public void violationExe(Integer id, Integer score) {
        studentMapper.updateViolation(id,score);
    }
}
